package com.goods.controller.business;

import com.goods.business.service.CategoriesServices;
import com.goods.common.model.business.ProductCategoryTreeNode;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author ：MJS
 * @date ：Created in 2021/6/7  14:21
 * @description：TODO
 */
@Api(tags = "业务管理-物资类别")
@RestController
@RequestMapping("/business/productCategory")
public class CategoriesController {
    @Autowired
    private CategoriesServices categoriesServices;

    //加载分类数据
    @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> categoryTree(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                              @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
                                                              ProductCategoryTreeNodeVO productCategoryTreeNodeVO){
        PageVO<ProductCategoryTreeNodeVO> categoriesList = categoriesServices.findCategories(pageNum, pageSize, productCategoryTreeNodeVO);
        return ResponseBean.success(categoriesList);
    }
    //加载父级分类数据
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO>productCategoryTreeNodeVOList=categoriesServices.findParentCategoryTree();
        return ResponseBean.success(productCategoryTreeNodeVOList);
    }
    //添加分类
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategoryTreeNodeVO productCategoryTreeNodeVO){
        categoriesServices.add(productCategoryTreeNodeVO);
        return ResponseBean.success();
    }

    //分类数据回显
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        ProductCategoryTreeNode productCategoryTreeNode=categoriesServices.edit(id);
        return ResponseBean.success(productCategoryTreeNode);
    }
    //修改分类数据
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,@RequestBody ProductCategoryTreeNode productCategoryTreeNode){
        categoriesServices.update(id,productCategoryTreeNode);
        return ResponseBean.success();
    }
    //删除数据
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        boolean flag=categoriesServices.delete(id);
        Map<String,String>map=new HashMap<>();
        map.put("errorMsg","关联数据不可被删除");
        if(flag){
        return ResponseBean.success();
        }else{
            return ResponseBean.error(map);
        }
    }
}
